<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * files actions.
 *
 * @package    FinalsClub
 * @subpackage files
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class filesActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
    }

    
    /**
     * Executes getuploadurl action
     *
     * @param sfRequest $request A request object
     */
    public function executeGetuploadurl(sfWebRequest $request)
    {

	$annotate_api = new AnnotateApi();
	$user = UserPeer::retrieveById($this->getUser()->getId());
	$upload_url = $annotate_api->getFileUploadURL($user->getEmail());
	
	return $this->renderText($upload_url);
    }
    
    
    /**
     * Executes delete action - to delete a user document
     *
     * @param sfRequest $request A request object
     */
    public function executeDelete(sfWebRequest $request)
    {
	$date = $request->getParameter('d');
	$code = $request->getParameter('c');
	$annotate_api = new AnnotateApi();
	$user = UserPeer::retrieveById($this->getUser()->getId());
	$result = $annotate_api->deleteDocument($user->getEmail(), $date, $code);

	// must delete ref to file in finalsclub db
	$file = FilePeer::retrieveByCodeAndOwnerId($code,$this->getUser()->getId());
	
	if($file) {
		
		$file->delete();
	}
	
	return $this->renderText($result);
    }
    
    /**
     * Executes getinnerlist action
     *
     * @param sfRequest $request A request object
     */
    public function executeGetinnerlist(sfWebRequest $request)
    {
	$annotate_api = new AnnotateApi();
	$this->annotate_base_url = $annotate_api->getAPIBaseURL();
	$this->user = UserPeer::retrieveById($this->getUser()->getId());
	$this->groupid = $request->getParameter('group');
	if($this->groupid) {
		$this->documents = FilePeer::retrieveByGroupId($this->groupid);
	} else {
		$this->documents = FilePeer::retrieveByOwnerId($this->getUser()->getId());
	}
	
	return $this->renderPartial('files/innerlist');
    }
    
    /**
     * Executes add action
     *
     * @param sfRequest $request A request object
     */
    public function executeAdd(sfWebRequest $request)
    {
	$date = $request->getParameter('d');  // this is the date from A.nnotate for the newly uploaded file
	$code = $request->getParameter('c');  // this is the document code for the newly uploaded file
	$status = $request->getParameter('status');  // this will be set to "OK", or "ERR" if there was a problem.
	$annotate_user = $request->getParameter('annotateuser');  // the email of the user (we should check that this is the same as the logged in user)
	$desired_access = $request->getParameter('access'); // will be 'every' for everyone
	$desired_permission = $request->getParameter('permit'); 
	$category = $request->getParameter('category');
	$file_name = $request->getParameter('name');
	$course_id = $request->getParameter('course_id');
	
	if(!$desired_permission) {
		$desired_permission = 0;
	}
	$group = $request->getParameter('group');

	if ($status == "OK") {
	    $annotate_api = new AnnotateApi();
	    $result = $annotate_api->listNotes($annotate_user, $date, $code, true);

	    if ($result) {

		// parse file info into a php nested array structure
		$info = json_decode($result, true);

		// do the database update here
		$file = new File();
		$file->setOwnerId($this->getUser()->getId());  // owner
		$file->setCode($code); // code
		$file->setAnnotateDate($date); // annotate d variable
		$file->setCreatedAt(time()); // when it was created
		$file->setSize($info["meta"]["srcsize"]); // size of file
		
		if($file_name)
		{
			$file->setName($file_name); // filename
		}
		else
		{	
			$file->setName($info["meta"]["srcname"]); // filename
		}
		
		
		$file->setTags($info["meta"]["tags"]);  // tags
		$file->setThumbnail($info["meta"]["thumb"]); // thumbnail file name
		$file->setDescription($info["meta"]["notes"]); // description
		$file->setNetworkId($this->getUser()->getNetworkId());
		$file->setPermissionLevel($desired_permission);
		$file->setCategory($category);
		
		if($group) $file->setGroupId($group);
		
		if($course_id) $file->setCourseId($course_id);
		
		// for now set link to viewable file via annotate	
		$user = UserPeer::retrieveById($this->getUser()->getId());

		// $file->setLink(); // this field TBD

		// we do not deal with access right now.  this is where access will be dealt with once we put that in

		// save file ref
		$file->save();		
	
	    } else {
		// some problem retrieving file info
	    }
	} else {
	    // an error occured in processing the uploaded file...could have been an a.nnotate account problem?
	    // redirect to the current page but show an error
	}
	return $this->renderText("");
    }
    
    /**
     * Executes download action
     *
     * @param sfRequest $request A request object
     */
    public function executeDownload(sfWebRequest $request)
    {
	$date = $request->getParameter('d');
	$code = $request->getParameter('c');
	$filename = $request->getParameter('f');
	$annotate_api = new AnnotateApi();
	$annotate_url = $annotate_api->getBaseURL();
	$download_url = $annotate_url.'/fc/download.php?d='.$date.'&c='.$code.'&f='.urlencode($filename);
	$this->redirect($download_url);
    }
    
    public function executeShow(sfWebRequest $request) {
	//	$this->forward404Unless($note = NotePeer::retrieveByPk($request->getParameter('id')), sprintf('Those notes do not exist for id (%s).', $request->getParameter('id')));
	$date = $request->getParameter('d');
	$code = $request->getParameter('c');
	$user = UserPeer::retrieveById($this->getUser()->getId());
	$annotate_api = new AnnotateApi();
	$this->annotate_url =  $annotate_api->getLoginDisplayFileURL($user->getEmail(), $date, $code);
    }
    
    
}
